﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.eforceglobal.QuickSearch.Command.ConfigParser;
using com.eforceglobal.QuickSearch.Command.Contracts;
using com.eforceglobal.QuickSearch.Command.DTO;

namespace com.eforceglobal.QuickSearch.Command
{
    public class SearchCommandHandler : IHttpHandler
    {
        private ISearchCommand searchCommand = null;

        public void ProcessRequest(HttpContext context)
        {
            string commandName = context.Request.RequestContext.RouteData.Values["CommandName"].ToString();
            string type = CommandConfigParser.GetCommandTypeByName(commandName);
            Type dbType = Type.GetType(type);
            if (dbType != null)
                searchCommand = Activator.CreateInstance(dbType) as ISearchCommand;
            List<ParameterInfo> parameters = (from keyValuePair in context.Request.RequestContext.RouteData.Values
                                              where !keyValuePair.Key.Equals("CommandName")
                                              select new ParameterInfo
                                                         {
                                                             ParameterName = keyValuePair.Key,
                                                             Value = keyValuePair.Value == null ? null : keyValuePair.Value.ToString()
                                                         }).ToList();
            searchCommand.InvokeCommand(commandName, parameters, context);

        }
        public bool IsReusable
        {
            get { return false; }
        }
    }
}
